<?php
if (! isset($_SESSION)) {
    session_start();
}
include (ZH_PATH . DS . MANAGERFPATH . DS . 'islogin' . ZH);
?>
  <script type="text/javascript" src="/common/hui/lib/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="/common/hui/lib/layer/3.1.1/layer.js"></script>
  <script type="text/javascript" src="/common/hui/static/h-ui/js/H-ui.js"></script>
  <script type="text/javascript" src="/common/hui/lib/jquery.contextmenu/jquery.contextmenu.r2.js"></script>
  <script type="text/javascript" src="/common/hui/static/h-ui.admin.pro.iframe/js/h-ui.admin.pro.iframe.js"></script>
<script type="text/javascript">
<?php 
//获取当前用户的权限
$qianxian_arr=explode(",", $_SESSION['Master_Power']);

//获取当前用户是不是总站的管理员
if($_SESSION['adminid']=="1734646461766828032")
{
$map=" pid='0' ";
}
else 
{
    $map=" pid='0' and id!='1734646461766828032'";
}
$map=" pid='0' ";
// 生成菜单
$str_leftmenu = array();
$str_headmenu = array();
$sql = "select id,title,url,menuname from zhmvc_admin_menu where pid='0'";

$rs = D("zhmvc_admin_menu")->field("id,title,url,menuname")->where($map)->order(' id asc')->getLinkAll("", true);
$map = null;
$datas = $rs['datas'];
$rows = $rs['rows'];
for ($i = 0; $i < $rows; $i ++) {
    $data = $datas[$i];
    // $sql1 = "select id,title,url,menuname from " . $db_pre . "admin_menu where pid=:pid";
    $map=" pid='".$data['id']."'";
   
    $rs1 = D("zhmvc_admin_menu")->field("id,title,url,menuname")
    ->where($map)->order(' id asc')
    ->getLinkAll("", true);
    $map = null;
    $datas1 = $rs1['datas'];
    $rows1 = $rs1['rows'];
    // echo $sql;
    if ($rows1 > 0) {
        
        $ismenuprint=false;
        $qianxian_arr_num=count($qianxian_arr);
        for($m=0;$m<$qianxian_arr_num;$m++)
        {
            for ($n = 0; $n < $rows1; $n ++) {
                $data2 = $datas1[$n];
                //echo $data2['menuname'].$data2['url']."<br>\n";
                
                if($qianxian_arr[$m]===$data2['menuname'].$data2['url'])
                {
                    $ismenuprint=true;
                    break 2;
                }
            }
        }
        
        if($ismenuprint==true)
        {
            $str_leftmenu[$i][0]['id'] = $data['id'];
            $str_leftmenu[$i][0]['title'] = $data['title'];
            $str_leftmenu[$i][0]['url'] = $data['url'];
            $str_leftmenu[$i][0]['menuname'] = str_replace('_', '', $data['menuname']);
            $str_headmenu[$i]['id'] = $data['id'];
            $str_headmenu[$i]['title'] = $data['title'];
            $str_headmenu[$i]['url'] = $data['url'];
            $str_headmenu[$i]['menuname'] = str_replace('_', '', $data['menuname']);
            
            for ($j = 0; $j < $rows1; $j ++) {
                $data1 = $datas1[$j];
               // echo "\n\n\n==========================\n";
               // var_dump($data1['menuname'].$data1['url']);
                //echo "\n==========================\n\n\n";
                for ($n = 0; $n < $qianxian_arr_num; $n ++) {
                  //  var_dump($qianxian_arr[$n]);
                 //   echo "\n--------------------------\n";
                    if($qianxian_arr[$n]==$data1['menuname'].$data1['url'])
                    {
                        $str_leftmenu[$i][($j + 1)]['id'] = $data1['id'];
                        $str_leftmenu[$i][($j + 1)]['title'] = $data1['title'];
                        $str_leftmenu[$i][($j + 1)]['url'] = $data1['url'];
                        $str_leftmenu[$i][($j + 1)]['menuname'] = str_replace('_', '', $data1['menuname']);
                    }
                }
 
                // $str_leftmenu[$i][($j + 1)] = "<a href=\"" . $data1['url'] . "\" target=\"main\">" . $data1['title'] . "</a>";
            }
        }
    }
}

$str_headmenu=array_values($str_headmenu);
$str_leftmenu=array_values($str_leftmenu);
//print_r($str_headmenu);
$count_leftheadmenu = count($str_headmenu);
$temp='
function getMenu() {
    var menuData = [
';
for($i=0;$i<$count_leftheadmenu;$i++)
{
    
    //一级栏目管理
    $temp.="{
      id: '".$str_headmenu[$i]['id']."',
      name: '".$str_headmenu[$i]['title']."',
	  linkPath: '#',
      icon: '&#xe616;',
      parentLinkId: '0',
    ";
    $temp_left=$str_leftmenu[$i];
    $temp_left=array_values($temp_left);
    
    $count_t_l=count($temp_left);
    //print_r($temp_left);
    if($count_t_l>1)
    {
        $temp.="
            subMenus: [
          ";
        for($j=1;$j<$count_t_l;$j++)
        {
            $temp.="{
                  id: '".$temp_left[$j]['id']."',
                  name: '".$temp_left[$j]['title']."',
            	  linkPath: '".$temp_left[$j]['url']."',
                  icon: '&#xe616;',
                  parentLinkId: '".$str_headmenu[$i]['id']."',
              },";
        }
        $temp = substr($temp,0,strlen($temp)-1);
        $temp.="]
            },";
    }
    else
    {
        $temp.="
            },";
    }
}
$temp = substr($temp,0,strlen($temp)-1);
$temp.="
    ]";
$temp.="
var pathname = window.location.pathname;

  var str = '';
  recursion(menuData);
  $('.Hui-admin-menu-dropdown .Hui-menu').append(str);

  function recursion(menuData) {
    if(menuData && menuData.length > 0) {
      for (var i in menuData) {
        if(menuData[i].subMenus && menuData[i].subMenus.length > 0) {
          str += 
          '<li class=\"Hui-menu-item\">'+
            '<div class=\"Hui-menu-title\"><i class=\"Hui-iconfont\">'+menuData[i].icon+'</i> '+menuData[i].name+'<i class=\"Hui-iconfont Hui-admin-menu-dropdown-arrow\">&#xe6d5;</i></div>'+
            '<ul class=\"Hui-menu-item-sub\">';
          recursion(menuData[i].subMenus);
          str += '</ul></li>';
        } else {
          str+= '<li class=\"Hui-menu-item\">';
          if(pathname == menuData[i].linkPath) {
            str+= '<div class=\"Hui-menu-title current\">';
            pid.push(parseInt(i))
          } else {
            str+= '<div class=\"Hui-menu-title\">';
          }
          str += '<a data-href=\"'+menuData[i].linkPath+'\" data-title=\"'+menuData[i].name+'\" href=\"javascript:void(0)\"><i class=\"Hui-iconfont\">'+menuData[i].icon+'</i> '+menuData[i].name+'</a></div><ul class=\"Hui-menu-item-sub\"></ul></li>';
        }
      }
    }
  }

  $(\".Hui-admin-menu-dropdown .Hui-menu\").Huifold({
		titCell: '.Hui-menu-title',
    mainCell: '.Hui-menu-item-sub',
    type: 3,
    className: 'open',
    item: '.Hui-menu-item-first',
	});
";
$temp.="
}

// 提取URL中参数为对象
function parseQueryString(url){
  var str = url.split('?')[1];
  var result = {};
  var temp = (str || '').split('&');
  for(var i=0; i<temp.length; i++){
    var temp2 = (temp[i] || '').split('=');
    result[temp2[0]] = temp2[1];
  }
  return result;
}
";
echo $temp;
?>
  $(function() {
    getMenu();
  });
</script>